package com.qf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.cilents.CourseCilents;
import com.qf.config.RedisKey;
import com.qf.pojo.reqVo.ShopcartReq;
import com.qf.pojo.respVo.BaseResp;
import com.qf.pojo.vo.entity.QfCourse;
import com.qf.service.ShopCartService;
import com.qf.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: 陌人不故
 * @Date: 2022/10/29/14:13
 */
@Service
public class ShopCartServiceImpl implements ShopCartService{

    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    CourseCilents courseCilents;

    @Override
    public BaseResp add(HttpServletRequest request, ShopcartReq shopcartReq) {
        String userId = getUserId(request);
        //从redis中判断该用户是否有对应的课程
        Boolean aBoolean = redisTemplate.boundHashOps(RedisKey.SHOP_CART_ + userId).hasKey(shopcartReq.getCourseid().toString());
        if (aBoolean){
            return BaseResp.FAIL("该课程以存在,无需重复添加!");
        }
        //如果没有
        BaseResp courseDetailById = courseCilents.findCourseDetailById(shopcartReq.getCourseid());
        if (courseDetailById.getCode() == 1){
            Object data = courseDetailById.getData();
            ObjectMapper mapper = new ObjectMapper();
            QfCourse qfCourse = mapper.convertValue(data, QfCourse.class);
            redisTemplate.boundHashOps(RedisKey.SHOP_CART_ + userId).put(shopcartReq.getCourseid().toString(),courseDetailById.getData());
            return BaseResp.SUCCESS(qfCourse.getCourseName()+"添加成功!",null,null);
        }
        return BaseResp.FAIL("该课程已下架!");
    }

    @Override
    public BaseResp findShopCardByToken(HttpServletRequest request) {
        String userId = getUserId(request);
        List values = redisTemplate.boundHashOps(RedisKey.SHOP_CART_ + userId).values();
        if (values != null && values.size() > 0){
            return BaseResp.SUCCESS("查询成功!",values,null);
        }
        return BaseResp.FAIL("您还尚未添加任何课程!");
    }

    @Override
    public BaseResp delShop(HttpServletRequest request, ShopcartReq shopcartReq) {
        String userId = getUserId(request);
        Object o = redisTemplate.boundHashOps(RedisKey.SHOP_CART_ + userId).get(shopcartReq.getCourseid().toString());
        if (o == null){
            return BaseResp.FAIL("商品已被移除!");
        }else {
            redisTemplate.boundHashOps(RedisKey.SHOP_CART_ + userId).delete(shopcartReq.getCourseid().toString());
            return BaseResp.SUCCESS("商品移除成功!",null,null);
        }
    }


    //获得用户id
    public String getUserId(HttpServletRequest request){
        String token = request.getHeader("token");
        String jwt = redisTemplate.opsForValue().get(RedisKey.LOGIN_KEY + token).toString();
        String userId = JWTUtils.verfiyTokenGetUserId(jwt);
        return userId;
    }
}
